package com.ruoyi.web.controller.warning;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.warning.domain.entity.FirewallLog;
import com.ruoyi.warning.domain.query.PortQuery;
import com.ruoyi.warning.service.IFirewallLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 防火墙相关 前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-07-24
 */
@RestController
@RequestMapping("/firewallLog")
@Component("firewallLog")
@Api(tags = "防火墙日志", description = "用于处理防火墙日志相关操作的控制器")
public class FirewallLogController {
    @Resource
    private IFirewallLogService firewallLogService;

    @ApiOperation("更新防火墙数据")
    @GetMapping(value = "/refresh")
    public R refresh()
    {
        return R.ok(firewallLogService.refresh());
    }

    @ApiOperation("分页获取防火墙历史数据")
    @GetMapping(value = "/getFirewallLog")
    public R getFirewallLog(PortQuery query)
    {
        Page<FirewallLog> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<FirewallLog> list = firewallLogService.list(new LambdaQueryWrapper<FirewallLog>()
                .eq(FirewallLog::getHostname, query.getHostname())
                .ge(query.getStartTime()!=null,FirewallLog::getCreateTime,query.getStartTime())
                .le(query.getEndTime()!=null,FirewallLog::getCreateTime,query.getEndTime())
                .orderByDesc(FirewallLog::getCreateTime));
        TableDataInfo tableDataInfo = new TableDataInfo(list, page.getTotal());
        return R.ok(tableDataInfo);
    }

    @PostMapping(value = "/exportExcel", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void exportExcel(@ModelAttribute PortQuery query, HttpServletResponse response) throws IOException {
        List<FirewallLog> list = firewallLogService.list(new QueryWrapper<FirewallLog>().lambda()
                .eq(FirewallLog::getDeviceId,query.getDeviceId())
                .ge(query.getStartTime()!=null,FirewallLog::getCreateTime,query.getStartTime())
                .le(query.getEndTime()!=null,FirewallLog::getCreateTime,query.getEndTime())
                .orderByAsc(FirewallLog::getCreateTime));
        ExcelUtil<FirewallLog> util = new ExcelUtil<>(FirewallLog.class);
        util.exportExcel(response, list, "资产表");
    }


}
